<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/2/1
 * Time: 17:54
 */

namespace Home\Event;
use Think\Controller;

class UsercenterEvent extends Controller
{
    //在我的订单中付款
    public function commitOrder(){
        //获得用户id
        $user_id = session("home")['id'];
        //获得订单唯一id
        $order_id = I("get.order_id");
        //通过订单id查看订单数据
        $order = M("order");
        $orderInfo = $order->where("order_id='$order_id' and user_id=$user_id")
            ->find();
        //找到地址id
        $address_id = $orderInfo['address_id'];
        $address = M("useradd");
        $addressInfo = $address->where("id=$address_id and user_id=$user_id")->find();
        $data["address_username"] = $addressInfo['name'];
        $data["address_name"] = $addressInfo['address'];
        $data["address_tel"] = $addressInfo['tel'];
        if($orderInfo["is_coupon"] == 1){//优惠券
            //获得优惠券id
            $coupon_id = $orderInfo['coupon_id'];
            //通过优惠券id查询减免价格
            $coupon = M('coupon');
            $data["is_coupon"] = $coupon->getFieldById($coupon_id,"cou_pri");
        }else{
            $data["is_coupon"] = 0;
        }
        $data["is_integral"] = $addressInfo['is_integral'];
        if($orderInfo["is_integral"] == 1){//积分
            $data["is_integral"] = $orderInfo['integral_num']/100;
        }
        $data["total"] = $orderInfo['price'];
        $data["order_id"] = $order_id;
        return $data;
    }

    //修改头像
    public function updateImg()
    {
        $user_id = session("home")['id'];
        //上传图片,把图片名放入shopping_user
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        $upload->savePath = 'user/'; // 设置附件上传（子）目录
        // 上传文件
        $info = $upload->upload();

        if (!$info) {// 上传错误提示错误信息
            return $upload->getError();
            die;
        }
        //获得添加图片的文件名
        $basename = $info['photo']['savename'];
        //获得图片的路径
        $basepath = $info['photo']['savepath'];
        //图片的路径
        $ImgName = get_img_url("./Uploads/user/", $basename, 0);
        //把商品图片进行缩略图显示

        $image = new \Think\Image();
        $image->open($ImgName);
        // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
        //生成保存路径
        $saveImg = "./Uploads/" . $basepath;
        //进行缩放
        $_50 = $image->thumb(50, 50)->save($saveImg . "50_" . $basename);
        if (!$_50) {
            @unlink($ImgName);
            @unlink($saveImg . "50_" . $basename);
            $this->error("图片缩放失败");
            die;
        }
        //把图片写入到数据库
        //1.首先查询有没有头像,如果要先把原图像文件删除,在写入新图像
        $user = M("user");
        $user_img = $user->getFieldByid($user_id,"user_img");

        if(!empty($user_img)){
            //先删除
            $imgpath = get_img_url("./Uploads/user/",$user_img,0);
            $imgpath_50 = get_img_url("./Uploads/user/",$user_img,50);
            @unlink($imgpath);
            @unlink($imgpath_50);
        }
        //更新数据库
        $row = $user->where("id=$user_id")->setField("user_img",$basename);
        if($row>0){
            $_SESSION['home']['user_img']= $basename;
        }
        return $row;
    }


    //确认收货
    public function receiveGood()
    {
        //订单订单id
        $order_id = I("get.order_id");
        //把is_deliver 改为2;
        $order = M("order");
        $row = $order->where("order_id='$order_id'")->setField("is_deliver",2);
        return $row;
    }

    //我的团购订单
    public function grouporder()
    {
        //得到用户id
        $user_id = session("home")["id"];
        //根据用户id查出他的所有团购订单
        $group = M("grouporder");
        $res = $group->alias("o")->join("shopping_goods g on g.id=o.goods_id")
            ->join("shopping_goodsimg i on i.goods_id=o.goods_id")
            ->where("o.user_id=$user_id and i.is_main=1")
            ->field(array("g.id","g.name","o.price","o.total_num","o.gnum","o.uid","o.is_pay","o.is_deliver","o.is_ok","o.is_status"))
            ->select();
        return $res;
    }
}